import { commonHttp } from '@/api/http';
import { AxiosRequestConfig } from 'axios';

export const createHttp = (defHttp = commonHttp) => {
  return <T>(
    url: string,
    config: AxiosRequestConfig,
    isReturnDirectly = true
  ): Promise<T> => {
    config = { ...config, url };
    return new Promise<T>((resolve, reject) => {
      defHttp
        .request(config)
        .then((res) => {
          // 如果需要直接返回data数据并且状态码为ok
          if (isReturnDirectly) {
            return resolve(res.data.data);
          }
          return resolve(res.data);
        })
        .catch((err) => {
          return reject(err);
        });
    });
  };
};

export const request = createHttp();
